<!DOCTYPE html>
<html>
<head>
    <title>吉垚物业后台管理系统</title>
    <meta charset="utf-8">
    <link rel="shortcut icon" th:href="./images/favicon.ico" type="image/x-icon">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <!-- 引入css -->
    <link rel="stylesheet" href="./css/plugins/font-awesome-4.7.0/css/font-awesome.min.css"  media="all">
    <link rel="stylesheet" href="./lib/layui-v2.3.0/css/layui.css"  media="all">
    <link rel="stylesheet" href="./css/main.css"  media="all">
    <link rel="stylesheet" href="./lib/zTree_v3/css/zTreeStyle/zTreeStyle.css" type="text/css">
</head>
<body class="timo-layout-page">
    <div class="layui-card">
        <div class="layui-card-header timo-card-header">
            <span><i class="fa fa-bars"></i> 类型归属管理</span>
            <i class="layui-icon layui-icon-refresh refresh-btn"></i>
        </div>
        <div class="layui-card-body">
            <div class="layui-row timo-card-screen put-row">
                <div class="pull-right screen-btn-group">
<!--                    <button class="layui-btn open-popup" data-title="添加角色" data-size="460,357" data-url="popupType.html">-->
                    <div class="btn-group-right">
                        <button class="layui-btn open-popup" data-title="添加类型" data-size="600,520" data-url="popupBelong.html">
                            <i class="fa fa-plus"></i> 添加</button>
    <!--                    <button class="layui-btn open-popup-param" data-type="radio" data-title="授权管理" data-size="600,500" data-url="popupType.html">-->
    <!--                        <i class="fa fa-user-secret"></i> 授权</button>-->
<!--                        <div class="btn-group">-->
<!--                            <button class="layui-btn">操作<span class="caret"></span></button>-->
<!--                            <dl class="layui-nav-child layui-anim layui-anim-upbit">-->
<!--                                <dd><a class="ajax-status" href="/role/status/ok">启用</a></dd>-->
<!--                                <dd><a class="ajax-status" href="/role/status/freezed">冻结</a></dd>-->
<!--                                <dd><a class="ajax-status" href="/role/status/delete">删除</a></dd>-->
<!--                            </dl>-->
<!--                        </div>-->
                    </div>
                </div>
            </div>
            <table class="layui-table">
                <thead>
                <tr>
                    <th>归属名称</th>
                    <th>创建时间</th>
                    <th>更新时间</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody id="belongs">
                </tbody>
            </table>
        </div>

    </div>
<script src="./lib/layui-v2.3.0/layui.js" charset="utf-8"></script>
<script src="./js/main.js" charset="utf-8"></script>
<script src="./js/plugins/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
    getBelongs()
    function getBelongs() {
        $('#belongs').empty();
        $.ajax({
            url:'/admin/getBelongs.do',
            headers:{
                accessToken:localStorage.getItem('accessToken')
            },
            success:function (data) {
                if (data.resMsg==undefined)
                    data=JSON.parse(data);
                if (data.code==401||data.code==402){
                    localStorage.clear();
                    sessionStorage.clear();
                    top.location.href='notlogin.html';
                    this.close();
                }
                if (data.resMsg=='用户未登录'){
                    sessionStorage.clear();
                    top.location.href='notlogin.html';
                    this.close();
                }
                for (var i=0;i<data.result.length;i++) {
                    var n=i+1;
                    $('#belongs').append('<tr>\n' +
                        // '                    <td><label class="timo-checkbox"><input type="checkbox" value="1">\n' +
                        // '                        <i class="layui-icon layui-icon-ok"></i></label></td>\n' +
                        '                    <td>'+data.result[i]['name']+'</td>\n' +
                        '                    <td>'+data.result[i]['createtime']+'</td>\n' +
                        '                    <td>'+data.result[i]['updatetime']+'</td>\n' +
                        '                    <td>\n' +
                        '                        <a class="open-popup" data-title="编辑类型" data-size="460,357" href="#" onclick="setVal('+data.result[i]['id']+',\''+data.result[i]['name']+'\')" data-url="popupUpdateBelong.html">编辑</a>\n' +
                        '                        <a href="#" onclick="delBelong('+data.result[i]['id']+')">删除</a>\n' +
                        '                    </td>\n' +
                        '                </tr>')
                }
            }
        })
    }

    function setVal(belid,name) {
        console.log('进入setVal')
        sessionStorage.setItem('belid',belid);
        sessionStorage.setItem('name',name);
    }

    function delBelong(belid) {
        if (!confirm('确认删除?'))
            return;
        $.ajax({
            url:'/admin/delBelong.do?belid='+belid,
            headers:{
                accessToken:localStorage.getItem('accessToken')
            },
            success:function (data) {
                if (data.resMsg==undefined)
                    data=JSON.parse(data);
                if (data.code==401||data.code==402){
                    localStorage.clear();
                    sessionStorage.clear();
                    top.location.href='notlogin.html';
                    this.close();
                }
                if (data.resMsg=='删除成功'){
                    getBelongs();
                }else{
                    alert('删除失败');
                }
            }
        })
    }

    function addBelongs() {
        var name=$('#belName').val();
        $.ajax({
            url:'/admin/addBelongs.do?name='+name,
            headers:{
                accessToken:localStorage.getItem('accessToken')
            },
            success:function (data) {
                if (data.resMsg==undefined)
                    data=JSON.parse(data);
                if (data.code==401||data.code==402){
                    localStorage.clear();
                    sessionStorage.clear();
                    top.location.href='notlogin.html';
                    this.close();
                }
                if (data.resMsg=='添加成功'){
                    getBelongs();
                }else{
                    alert('添加失败');
                }
            }
        })
    }
</script>
</body>
</html>